package com.abbas.CayTcore.ui

import android.app.AlertDialog
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import com.abbas.CayTcore.base.BaseFragment
import com.abbas.CayTcore.databinding.FragmentCoreDetailBinding
import com.abbas.CayTcore.entity.TCoreBase
import com.abbas.CayTcore.presenter.CoreDetailPresenter
import com.abbas.CayTcore.startActivity
import com.abbas.CayTcore.view.ViewContract

class CoreDetailFragment(private val coreDetail: TCoreBase) :
    BaseFragment<ViewContract.CoreDetailView, CoreDetailPresenter, FragmentCoreDetailBinding>(),ViewContract.CoreDetailView {

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmentCoreDetailBinding =
        FragmentCoreDetailBinding.inflate(layoutInflater, container, false)

    override fun initPresenter(): CoreDetailPresenter = CoreDetailPresenter()

    override fun initData() {
        coreDetail.apply {
            binding.name.text = Editable.Factory.getInstance().newEditable(this.toString())
            binding.textView.text = Editable.Factory.getInstance().newEditable(xh.toString())
            binding.cid.text = "序列号：" + "%04d".format(_id)
            binding.xcV.text = Editable.Factory.getInstance().newEditable(wireRod.toString())
            binding.qsv.text = Editable.Factory.getInstance().newEditable(this.qs.toString())
            dz.let {
                binding.dzE.editText?.text = Editable.Factory.getInstance().newEditable(it.toString())
                binding.fE.editText?.text = Editable.Factory.getInstance().newEditable((it * 50000).toString())
            }
        }
    }

    override fun initEvent() {
        binding.dzE.editText?.onFocusChangeListener = View.OnFocusChangeListener { p0, p1 ->
            when (p1) {
                true -> {

                }
                else -> {
                    presenter.updateCore(
                        coreDetail.apply {
                            dz = binding.dzE.editText?.text.toString().toFloat()
                        }
                    )
                }
            }
        }

        binding.weight.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                try {
                    p0?.toString()?.toFloat()?.let { weight ->
                        coreDetail.dz.let {
                            binding.count.setText(
                                (weight / it).toString()
                            )
                        }
                    }
                } catch (e: Exception) {

                }
            }

        })

        binding.count2.addTextChangedListener {
            it?.let {
                val cs = it.toString()
                if (cs.isNotBlank()) {
                   cs.toFloatOrNull()?.let { cf ->
                       binding.weight2.text = Editable.Factory.getInstance()
                           .newEditable(
                               "%.2f".format(cf * 1000 * coreDetail.dz)
                           )
                   }
                }
            }
        }

        binding.counter.setOnClickListener {
            requireActivity().startActivity(CounterResultActivity::class.java) {
                putExtra("core", coreDetail._id)
            }
        }

        binding.dzSave.setOnClickListener {
            presenter.updateCore(
                coreDetail.apply {
                    dz = binding.dzE.editText?.text?.toString()?.toFloat() ?: 0f
                }
            )
        }
        binding.saveCore.setOnClickListener {
            presenter.updateCore(
                coreDetail.apply {
                    parse(binding.name.text.toString())
                    wireRod.parse(binding.xcV.text.toString())
                    xh = binding.textView.text.toString()
                    qs = binding.qsv.text.toString().toFloat()
                    binding.dzE.editText?.text.toString().apply {
                        if (!TextUtils.isEmpty(this)) {
                            dz = this.toFloat()
                        }
                    }

                }
            )
        }

        binding.deleteCore.setOnClickListener {
            AlertDialog.Builder(requireContext())
                .setTitle("警告")
                .setMessage("删除后无法撤销操作")
                .setPositiveButton("OK"
                ) { p0, p1 ->
                    presenter.deleteCore(coreDetail)
                    p0.dismiss()
                }
                .create()
                .show()
        }
    }

    override fun onCoreUpdate() {
        with(parentFragment as ViewContract.WindingAllDataUpView){
            getAllCore()
        }
    }

    override fun onCoreDeleted() {
        with(parentFragment as ViewContract.WindingAllDataUpView){
            getAllCore()
        }
    }

}